package com.xgry.bpm.api.base.meetingdata.service.impl;

import com.xgry.bpm.api.base.meetingdata.entity.MeetingData;
import com.xgry.bpm.api.base.meetingdata.dao.MeetingDataDao;
import com.xgry.bpm.api.base.meetingdata.form.MeetingDataForm;
import com.xgry.bpm.api.base.meetingdata.service.MeetingDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xgry.bpm.api.base.meetingdata.vo.MeetingDataVO;
import org.springframework.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;


/**
 * <p>
 * MeetingData服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-16
 */
@Service
public class MeetingDataServiceImpl extends ServiceImpl<MeetingDataDao, MeetingData> implements MeetingDataService {

    @Override
    public int save(MeetingDataForm record) {
        MeetingData data = this.convertDO(record);
        return baseMapper.insert(data);
    }

    @Override
    public int updateById(MeetingDataForm record) {
        //修改时间
        MeetingData data = this.convertDO(record);
        return baseMapper.updateById(data);
    }

    @Override
    public int deleteById(String id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public MeetingDataVO selectById(String id) {
        MeetingData data = baseMapper.selectById(id);
        MeetingDataVO vo = new MeetingDataVO();
        BeanUtils.copyProperties(data, vo);
        return vo;
    }

    @Override
    public List<MeetingDataVO> selectAll(MeetingDataForm record) {
        MeetingData data = this.convertDO(record);
        QueryWrapper<MeetingData> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(data);
        List<MeetingData> list = baseMapper.selectList(queryWrapper);
        return this.convert(list);
    }

    @Override
    public IPage<MeetingDataVO> selectPage(MeetingDataForm record) {
        // form -> do 转换
        MeetingData data = this.convertDO(record);
        // 分页数据设置
        Page<MeetingData> page = new Page<>(record.getCurrent(), record.getSize());
        // 查询条件
        QueryWrapper<MeetingData> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(data);
        IPage<MeetingData> list = baseMapper.selectPage(page, queryWrapper);
        IPage<MeetingDataVO> iPage = new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
    }

    @Override
    public IPage<MeetingData> getPage(IPage<MeetingData> page, QueryWrapper<MeetingData> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<MeetingDataVO> selectPageToSummary(MeetingDataForm record) {
        // form -> do 转换
        MeetingData data = this.convertDO(record);
        // 分页数据设置
        Page<MeetingData> page = new Page<>(record.getCurrent(), record.getSize());
        // 查询条件
        QueryWrapper<MeetingData> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(data);
        queryWrapper.eq("type",1);
        IPage<MeetingData> list = baseMapper.selectPage(page, queryWrapper);
        IPage<MeetingDataVO> iPage = new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
    }

    @Override
    public IPage<MeetingDataVO> selectPageMeetingDataPage(MeetingDataForm record) {
        // form -> do 转换
        MeetingData data = this.convertDO(record);
        // 分页数据设置
        Page<MeetingData> page = new Page<>(record.getCurrent(), record.getSize());
        // 查询条件
        QueryWrapper<MeetingData> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(data);
        queryWrapper.eq("type",0);
        IPage<MeetingData> list = baseMapper.selectPage(page, queryWrapper);
        IPage<MeetingDataVO> iPage = new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
    }


    /**
     * Form -> Do
     *
     * @param form 对象
     * @return Do对象
     */
    private MeetingData convertDO(MeetingDataForm form) {
        MeetingData data = new MeetingData();
        BeanUtils.copyProperties(form, data);
        return data;
    }

    /**
     * Do -> VO
     *
     * @param list 对象
     * @return VO对象
     */
    private List<MeetingDataVO> convert(List<MeetingData> list) {
        List<MeetingDataVO> meetingDataList = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            return meetingDataList;
        }
        for (MeetingData source : list) {
            MeetingDataVO target = new MeetingDataVO();
            BeanUtils.copyProperties(source, target);
            meetingDataList.add(target);
        }
        return meetingDataList;
    }

}
